import router from "./router";
import store from "./store";
import { Message } from "element-ui";
import NProgress from "nprogress"; // progress bar
import "nprogress/nprogress.css"; // progress bar style
import { getToken } from "@/utils/auth"; // get token from cookie
import getPageTitle from "@/utils/get-page-title";

NProgress.configure({ showSpinner: false }); // NProgress Configuration

const whiteList = ["/login", "/auth-redirect", "/survey", "/gatherIssue", "/punchCard", '/constructionSiteBill', '/constructionSiteBillDetail']; // no redirect whitelist

router.beforeEach(async (to, from, next) => {
  // start progress bar
  NProgress.start();

  // 判断是否存在用户信息

  // 设置网页标签名称
  document.title = getPageTitle(to.meta.title);

  const userInfo = JSON.parse(sessionStorage.getItem("userInfo")) || {};

  // 判断当前去的地方是否为白名单相关页面 如果是 就放行
  if (whiteList.includes(to.path)) {
    next();
  } else {
    // 判断是否存在用户信息
    if (userInfo.token) {
      // 获取路由信息
      await store.dispatch("permission/generateRoutes", userInfo.roles);
      next();
    } else {
      next("/login");
      NProgress.done();
    }
  }
});

router.afterEach(() => {
  // finish progress bar
  NProgress.done();
});
